namespace YAF.Classes.Core
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;

    using YAF.Classes.Data;
    using YAF.Classes.Utils;

    public class ForumPage : UserControl
    {
        #region Variables
        private readonly Hashtable _pageCache;

        private bool _noDataBase;

        private bool _showToolBar = Config.ShowToolBar;

        private bool _showFooter = Config.ShowFooter;

        private readonly string _transPage = string.Empty;

        public event EventHandler<ForumPageRenderedArgs> ForumPageRendered;

        protected bool _isAdminPage;

        public bool IsAdminPage
        {
            get
            {
                return this._isAdminPage;
            }
        }

        protected bool _isRegisteredPage = false;

        public bool IsRegisteredPage
        {
            get
            {
                return this._isRegisteredPage;
            }
        }

        private bool _allowAsPopup = false;

        public bool AllowAsPopup
        {
            get
            {
                return this._allowAsPopup;
            }
            protected set
            {
                this._allowAsPopup = value;
            }
        }

        private IYafHeader _header = null;

        public IYafHeader ForumHeader
        {
            get
            {
                return this._header;
            }
            set
            {
                this._header = value;
            }
        }

        private IYafFooter _footer = null;

        public IYafFooter ForumFooter
        {
            get
            {
                return this._footer;
            }
            set
            {
                this._footer = value;
            }
        }
        #endregion

        #region Constructor and events
        public ForumPage()
            : this("")
        {
        }

        public ForumPage(string transPage)
        {
            this._pageCache = new Hashtable();

            this._transPage = transPage;
            this.Init += new EventHandler(this.ForumPage_Init);
            this.Load += new EventHandler(this.ForumPage_Load);
            this.Unload += new EventHandler(this.ForumPage_Unload);
            this.Error += new EventHandler(this.ForumPage_Error);
            this.PreRender += new EventHandler(this.ForumPage_PreRender);
        }

        private void ForumPage_Error(object sender, EventArgs e)
        {
            Exception x = this.Server.GetLastError();
            DB.eventlog_create(this.PageContext.PageUserID, this, x);
        }

        private void ForumPage_Init(object sender, EventArgs e)
        {
            YafContext.Current.ForumPageInit(this, new EventArgs());

            if (this._noDataBase)
            {
                return;
            }

#if DEBUG
            QueryCounter.Reset();
#endif

            YafContext.Current.InstanceFactory.GetInstance<LocalizationHandler>().TranslationPage = this._transPage;

            YafContext.Current.ForumPagePreLoad(this, new EventArgs());
        }

        private void ForumPage_Load(object sender, EventArgs e)
        {
            if (this.PageContext.BoardSettings.DoUrlReferrerSecurityCheck)
            {
                Security.CheckRequestValidity(this.Request);
            }
        }

        private void ForumPage_Unload(object sender, EventArgs e)
        {
            if (this._pageCache != null)
            {
                this._pageCache.Clear();
            }
        }
        #endregion

        #region Render Functions
        protected void InsertCssRefresh(Control addTo)
        {
            addTo.Controls.Add(ControlHelper.MakeCssIncludeControl(YafForumInfo.GetURLToResource("css/forum.css")));
            addTo.Controls.Add(ControlHelper.MakeCssIncludeControl(YafContext.Current.Theme.BuildThemePath("theme.css")));

            if (this.ForumHeader.RefreshURL != null && this.ForumHeader.RefreshTime >= 0)
            {
                var refresh = new HtmlMeta();
                refresh.HttpEquiv = "Refresh";
                refresh.Content = String.Format("{1};url={0}", this.ForumHeader.RefreshURL, this.ForumHeader.RefreshTime);

                addTo.Controls.Add(refresh);
            }
        }

        protected Control _topPageControl = null;

        public Control TopPageControl
        {
            get
            {
                if (this._topPageControl == null)
                {
                    if (this.Page != null && this.Page.Header != null)
                    {
                        this._topPageControl = this.Page.Header;
                    }
                    else
                    {
                        this._topPageControl = ControlHelper.FindControlRecursiveBoth(this, "YafHead") ??
                                this.ForumHeader.ThisControl;
                    }
                }

                return this._topPageControl;
            }
        }

        protected void SetupHeaderElements()
        {
            this.InsertCssRefresh(this.TopPageControl);
        }

        private void ForumPage_PreRender(object sender, EventArgs e)
        {
            this.SetupHeaderElements();
            this.ForumHeader.SimpleRender = !this.ShowToolBar;
            this.ForumFooter.SimpleRender = !this.ShowFooter;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);

            if (this.ForumPageRendered != null)
            {
                this.ForumPageRendered(this, new ForumPageRenderedArgs(writer));
            }
        }

        protected bool NoDataBase
        {
            set
            {
                _noDataBase = value;
            }
        }

        public void RedirectNoAccess()
        {
            YafServices.Permissions.HandleRequest(ViewPermissions.RegisteredUsers);
        }

        public Hashtable PageCache
        {
            get
            {
                return _pageCache;
            }
        }

        public string RefreshURL
        {
            get
            {
                if (ForumHeader != null)
                    return ForumHeader.RefreshURL;
                return null;
            }
            set
            {
                if (this.ForumHeader != null)
                    this.ForumHeader.RefreshURL = value;
            }
        }


        #endregion
    }
}
